package com.jl.tools.ui.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import com.jl.tools.util.CapFileInfo;

public class CapFileAnalyseDialog extends TitleAreaDialog {
	private static final String Message = "choise a cap file and click button analyse";
	
	private static String lastPath ;
	
	private Text textPath;
	private Text textOutput;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public CapFileAnalyseDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(SWT.DIALOG_TRIM | SWT.MIN);
	}
	
	

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayout(new GridLayout(2, false));
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		textPath = new Text(container, SWT.BORDER);
		textPath.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		textPath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button btnFilePath = new Button(container, SWT.NONE);
		btnFilePath.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_fiile_path();
			}
		});
		btnFilePath.setFont(SWTResourceManager.getFont("Courier New", 10, SWT.NORMAL));
		btnFilePath.setText("File Path");
		
		textOutput = new Text(container, SWT.BORDER | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL);
		textOutput.setFont(SWTResourceManager.getFont("Courier New", 11, SWT.NORMAL));
		textOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

		return area;
	}

	protected void event_click_btn_fiile_path() {
		FileDialog dia = new FileDialog(getShell(),SWT.OPEN);
		dia.setFilterPath(lastPath);
		dia.setFilterExtensions(new String[]{"*.cap"});
		String path = dia.open();
		if(path != null){
			this.textPath.setText(path);
			lastPath = path;
			event_click_btn_analyse();
		}
	}



	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button btnAnalyse = createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL,
				true);
		btnAnalyse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_analyse();
			}
		});
		btnAnalyse.setFont(SWTResourceManager.getFont("Courier New", 10, SWT.NORMAL));
		btnAnalyse.setText("Analyse");
		Button btnCopy = createButton(parent, IDialogConstants.NO_ID,
				IDialogConstants.NO_LABEL, false);
		btnCopy.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				event_click_btn_copy();
			}
		});
		btnCopy.setFont(SWTResourceManager.getFont("Courier New", 10, SWT.NORMAL));
		btnCopy.setText("Copy");
	}

	protected void event_click_btn_copy() {
		Clipboard clip = new Clipboard(Display.getDefault());
		clip.clearContents();
		clip.setContents(new Object[]{this.textOutput.getText()}, new Transfer[]{TextTransfer.getInstance()});
		
	}



	protected void event_click_btn_analyse() {
		String info = getCapFileInfo();
		this.textOutput.setText(info);
	}



	private String getCapFileInfo() {
		CapFileInfo info = new CapFileInfo(this.textPath.getText());
		return info.getInfomation();
	}



	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(495, 482);
	}
	
	@Override
	public void create() {
		super.create();
		this.setTitle("Analyse cap file");
		this.setMessage(Message);
	}
}
